package cn.com.msui.controller.manage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.msui.common.UUIDHexGenerator;
import cn.com.msui.controller.BaseController;
import cn.com.msui.controller.JsonResult;
import cn.com.msui.domain.ViewItem;
import cn.com.msui.domain.manage.Menu;
import cn.com.msui.service.manage.MenuService;

@Controller
@RequestMapping("/manage")
public class MenuController extends BaseController {

	private static Logger logger = Logger.getLogger(MenuController.class);
	private MenuService menuService;

	public MenuService getMenuService() {
		return menuService;
	}

	@Autowired
	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	@RequestMapping(value = "/menu")
	public String showMenuList(HttpServletRequest request) {
		return "manage/menu";
	}

	@RequestMapping(value = "/menu/manage")
	public String showMenuManage(HttpServletRequest request) {
		return "manage/menu_manage";
	}

	@RequestMapping(value = "/listMenus", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> listMenus(ViewItem view) {
		Map<String, Object> map = new HashMap<String, Object>();
		int totalCount = 0;
		List<Menu> menuList = new ArrayList<Menu>();
		try {
			totalCount = menuService.getTotalCount(null);
			if (totalCount > 0)
				menuList = menuService.getMenus(view);
			map.put("rows", menuList);
			map.put("total", totalCount);
		} catch (Exception e) {
			logger.debug(e);
			e.printStackTrace();
		}

		return map;
	}

	@RequestMapping(value = "/listMenuGroup", method = RequestMethod.POST)
	@ResponseBody
	public List<Menu> listMenuGroup() {
		List<Menu> menuGroupList = null;
		try {
			Menu menu = new Menu();
			menu.setParentId("");
			menuGroupList = menuService.getChildrenMenus(null);
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		return menuGroupList;
	}

	@RequestMapping(value = "/listChildrenMenu", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> listChildrenMenu(String groupId) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Menu> menuList = null;
		if (groupId != null && StringUtils.hasLength(groupId)) {
			try {
				Menu menu = new Menu();
				menu.setId(groupId);
				menuList = menuService.getChildrenMenus(menu);
			} catch (Exception e) {
				logger.error(e);
				e.printStackTrace();
			}
		}
		if (menuList != null) {
			map.put("total", menuList.size());
			map.put("rows", menuList);
		}
		return map;
	}

	@RequestMapping(value = "/removeMenus", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult removeMenus(String ids) {
		JsonResult json = new JsonResult(false, "目录删除失败！");
		try {
			if (ids != null && ids.length() > 0) {
				String[] menuIds = ids.split(",");
				menuService.removeMenus(menuIds);
				json.setSuccess(true);
				json.setMsg("目录删除成功！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		return json;
	}

	@RequestMapping(value = "/saveMenu")
	@ResponseBody
	public JsonResult saveMenu(Menu menu) {
		JsonResult json = new JsonResult(false, "目录保存失败！");
		json.setObj(menu);
		if (!StringUtils.hasText(menu.getParentId())) {
			menu.setParentId(null);
		}
		try {
			if (StringUtils.hasText(menu.getId())) {
				menuService.modifyMenu(menu);
			} else {
				menu.setId(UUIDHexGenerator.getInstance().generate());
				menuService.addMenu(menu);
			}
			json.setSuccess(true);
			json.setMsg("目录保存成功！");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		return json;
	}
}
